<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线代码编辑器</title>
    <style>
        textarea {
            overflow-y: auto;
            font-size: 22px;
        }
    </style>
</head>

<body>
    <div> <button type="button" id="btn">运行</button></div>

    <textarea id="code" rows="30" cols="80"></textarea>
    <div id="result"></div>

    <script>

        var scriptStr = `
function sum(num1, num2){
    return num1+ num2
}
return sum(10,20)
        `

        var codeEl = document.getElementById("code");
        var resultEl = document.getElementById("result");
        var btnEl = document.getElementById("btn");

        codeEl.value = scriptStr;

        function createFun(body) {
            return new Function(body)
        }


        btnEl.addEventListener("click", () => {
            const fn = createFun(codeEl.value);
            var result = fn();
            resultEl.innerHTML = result;
        })


    </script>
</body>

</html>